"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具显示客户端信息
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.dsscmd工具正常显示客户端信息
    2.dsscmd lscli加异常参数
    3.在集群停止的情况下执行
Expect      :
    1.成功
    2.合理报错
    3.显示成功
History     :
"""
import os
import re
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:dsscmd工具正常显示客户端信息;expect:显示成功-----"
        self.logger.info(step1)
        shell_cmd = f"source {macro.DB_ENV_PATH};ls -l $GAUSSHOME"
        self.logger.info(shell_cmd)
        shell_msg = self.common.get_sh_result(self.primary_node, shell_cmd)
        self.assertNotIn("ERROR", shell_msg, "执行失败" + step1)
        gausshome = shell_msg.split('->')[-1].strip()
        self.logger.info(gausshome)
        dsscmd_path = os.path.join(gausshome, 'bin', 'dsscmd')
        self.logger.info(dsscmd_path)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lscli"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        exp = f"cli_pid.*start_time.*process_name"
        search_res = re.search(exp, execute_msg, re.S)
        self.assertIsNotNone(search_res, '执行失败' + step1)
        self.assertIn(dsscmd_path, execute_msg, "执行失败" + step1)

        step2 = "-----step2:dsscmd lscli加异常参数;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = ['-t t', '-m B']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lscli {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("args num 4 error", execute_msg, "执行失败" + step2)

        step3 = "-----step3:在集群停止的情况下执行;expect:显示成功-----"
        self.logger.info(step3)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lscli"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        search_res = re.search(exp, execute_msg, re.S)
        self.assertIsNotNone(search_res, '执行失败' + step1)
        self.assertIn(dsscmd_path, execute_msg, "执行失败" + step1)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        self.assertIn('cluster_state   : Normal', status, '执行失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
